package controller;

import model.Major;
import service.MajorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;

@Controller
public class MajorController{
    private MajorService majorService;

    public void setMajorService(MajorService majorService){
        this.majorService=majorService;
    }

    @RequestMapping("/getAll_major")
    public ModelAndView getAll() throws Exception{
        ArrayList<Major> major=majorService.getAllMajors();
        ModelAndView modelAndView=new ModelAndView("show_major");
        modelAndView.addObject("Majors",major);
        return modelAndView;
    }

    @RequestMapping("/dalete_major")
    public String delete(HttpServletRequest request, HttpServletResponse response,
                         RedirectAttributes redirectAttributes) throws Exception{
        String majorId= request.getParameter("majorId");
        boolean success=majorService.deleteMajor(majorId);

        if(success){
            redirectAttributes.addFlashAttribute("succ", "删除成功！");
            return "redirect:/show_major.jsp";
        }else {
            redirectAttributes.addFlashAttribute("error", "删除失败！");
            return "redirect:/show_major.jsp";
        }

    }

    @RequestMapping("/get_major")
    public ModelAndView get(HttpServletRequest request,HttpServletResponse response) throws Exception{
        String majorId= request.getParameter("majorId");
        ArrayList<Major> major=majorService.getMajor(majorId);
        ModelAndView modelAndView=new ModelAndView("show_major1");
        modelAndView.addObject("Major",major);
        return modelAndView;
    }

    @RequestMapping("/edit_major")
    public ModelAndView edit(HttpServletRequest request,HttpServletResponse response) throws Exception{
        String majorId= request.getParameter("majorId");
        ArrayList<Major> major=majorService.getMajor(majorId);
        ModelAndView modelAndView=new ModelAndView("show_major");
        modelAndView.addObject("Major",major);
        return modelAndView;
    }

    @RequestMapping("/doEdit_major")
    public String doEdit(HttpServletRequest request,HttpServletResponse response,
                         int collegeId,String mName,int number,RedirectAttributes redirectAttributes) throws Exception{
        String majorId= request.getParameter("majorId");
        Major major=new Major();
        major.setMajorId(majorId);
        major.setCollegeId(collegeId);
        major.setmName(mName);
        major.setNumber(number);
        boolean succ=majorService.editMajor(major);
        if(succ){
            redirectAttributes.addFlashAttribute("succ", "修改成功！");
            return "redirect:/show_major.jsp";
        }else {
            redirectAttributes.addFlashAttribute("error", "修改失败！");
            return "redirect:/show_major.jsp";
        }

    }

    @RequestMapping("/add_major")
    public ModelAndView add()throws Exception{
        ModelAndView modelAndView=new ModelAndView("show_major");
        return modelAndView;

    }

    @RequestMapping("/doAdd_major")
    public String doAdd(String majorId,int collegeId,String mName,int number,RedirectAttributes redirectAttributes)throws Exception{
        Major major=new Major();
        major.setMajorId(majorId);
        major.setCollegeId(collegeId);
        major.setmName(mName);
        major.setNumber(number);
        boolean succ=majorService.addMajor(major);
        if (succ){
            redirectAttributes.addFlashAttribute("succ", "添加成功！");
            return "redirect:/show_major.jsp";
        }else {
            redirectAttributes.addFlashAttribute("succ", "添加失败！");
            return "redirect:/show_major.jsp";
        }
    }
}
